// pages/photoGallery/index/index.js
const app = getApp()
const router = require('../../../utils/router.js')
const api = require('../../../utils/request.js') 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    Custom: app.globalData.Custom,
    TabCur: 1,
    navScrollLeft: 0,
    currentLabelID: 1,
    imageHost: api.IMG_HOST,
    typeTabsHeight: 0,
    galleryLabelList: [],
    galleryList: []
  },
  /**
   * 初始化
   */
  init() {
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    return new Promise(resolve => {
      this.getGalleryLabelList().then(galleryLabelList => {
        this.setData({
          galleryLabelList: galleryLabelList,
          currentLabelID: galleryLabelList[0] ? galleryLabelList[0].ID : 1
        })
        this.getGalleryList().then((galleryList) => {
          this.setData({
            galleryList: galleryList
          })
          resolve()
        })
      })
    })
  },
  /**
   * 获取图册列表
   */
  getGalleryList() {
    return new Promise(resolve => {
      api._get('/User/Gallery/List', {
        index: this.data.currentLabelID
      }).then(res => {
        if (res.Code === 100000) {
          resolve(res.Data)
        }
      })
    })
  },
  /**
   * 选择标签
   */
  selectTagHandler({ currentTarget}) {
    this.setData({
      currentLabelID: currentTarget.dataset.labelid
    })
    this.getGalleryList().then(() => {
      this.setData({
        TabCur: null
      })
    })
  },
  /**
   * 关闭分类选择弹窗
   */
  closeTypeFilter () {
    if (this.data.TabCur != null) {
      this.setData({
        TabCur: null
      })  
    }
  },
  /**
   * 获取图片标签列表
   */
  getGalleryLabelList() {
    return new Promise(resolve => {
      api._get('/User/Gallery/Label/List', {
      }).then(res => {
        if (res.Code === 100000) {
          resolve(res.Data)
        }
      })
    })
  },
  /**
   * 选择tab
   */
  tabSelect(e) {
    this.setData({
      currentLabelID: e.currentTarget.dataset.id,
      navScrollLeft: (e.currentTarget.dataset.index - 1) * 60
    })
    this.getGalleryList().then((galleryList) => {
      this.setData({
        galleryList: galleryList
      })
    })
    // this.setData({
    //   galleryLabelList: []
    // })
    // const id = e.currentTarget.dataset.id
    // if (id === this.data.TabCur) {
    //   this.setData({
    //     TabCur: null
    //   })
    // } else {
    //   this.getGalleryLabelList().then(res => {
    //     this.setData({
    //       galleryLabelList: res.Data,
    //       TabCur: e.currentTarget.dataset.id
    //     })
    //   })
    // }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.init()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  tapNavigateToUrl({ currentTarget }) {
    router.push(currentTarget.dataset.url)
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.init().finally(() => {
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})